package com.googlecode.dgwt.client.dijit.form.base.view;

import com.googlecode.dgwt.client.dijit.Widget;
import com.googlecode.dgwt.client.dijit.base.HasScrollBars.ScrollBarVisibility;
import com.googlecode.dgwt.client.dijit.base.selection.SelectionMode;
import com.googlecode.dgwt.client.dijit.view.GridView;
import com.googlecode.dgwt.client.dijit.view.base.ViewBase;
import com.googlecode.dgwt.client.dijit.view.model.CheckBoxSelector;
import com.googlecode.dgwt.client.dijit.view.model.ColumnSelector;
import com.googlecode.dgwt.client.dijit.view.store.CellViewBase;
import com.googlecode.dgwt.client.dojo.data.base.ModeledStore;
import com.googlecode.dgwt.client.dojo.data.base.Store;
import com.googlecode.dgwt.client.dojo.data.listener.ModelListener;
import com.googlecode.dgwt.client.dojo.data.model.ColumnSet;
import com.googlecode.dgwt.client.dojo.data.model.StoreColumn;
import com.googlecode.dgwt.client.dojo.exception.AttachException;
import com.googlecode.dgwt.client.dojox.data.ProxyStore;
import com.googlecode.dgwt.client.dojox.data.base.EmptyStore;
import com.googlecode.dgwt.client.dojox.data.model.ProxyColumn;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class ListBoxView extends ViewBase {
    
    private final GridView cellView;

    private final ProxyStore<Object> proxy;
    
    private final ColumnSelector<Object> selector;

    private final ProxyColumn<Object,Object> textColumn;
    
    private final ModelListener<Object> modelListener;

    
    public ListBoxView(SelectionMode selectionMode) {
        cellView = new GridView();
        cellView.setScrollBars(ScrollBarVisibility.AUTO, 
                               ScrollBarVisibility.HIDDEN);
        cellView.getSelection().setMode(selectionMode);
        
        selector = new CheckBoxSelector<Object>();
        textColumn = new ProxyColumn<Object, Object>();
        
        proxy = new ProxyStore<Object>();
        proxy.getModel().getColumns().append(selector)
                                     .append(textColumn);
        
        modelListener = new ModelListener<Object>(){
            @Override
            public void onColumnAdded(StoreColumn<Object, ?> column) {
                onTextColumnChanged();
            }

            @Override
            public void onColumnRemoved(StoreColumn<Object, ?> column) {                
                onTextColumnChanged();
            }

            @Override
            public void onColumnReplaced(int columnIndex, StoreColumn<Object, ?> oldColumn, StoreColumn<Object, ?> newColumn) {
                onTextColumnChanged();
            }

            @Override
            public void onColumnChanged(StoreColumn<Object, ?> column) {
                // don't needs to validate the text column                
            }

            @Override
            public void onColumnRefresh(StoreColumn<Object, ?> column) {
                // don't needs to validate the text column                
            }

            @Override
            public void onAttach(Store<Object> parent) throws AttachException {
                // don't needs to validate the text column                
            }

            @Override
            public void onDetach(Store<Object> parent) {
                // don't needs to validate the text column                
            }            
        };
        
        cellView.setStore(proxy);
    }

    public void setStore(ModeledStore<Object> store){
        if (!getStore().equals(store)){
            getStore().removeListener(modelListener);
            proxy.setTargetStore(store);
            getStore().addListener(modelListener);
            onTextColumnChanged();
        }
    }
    
    protected void onTextColumnChanged() {
        textColumn.setTargetColumn(getTextColumn());
    }

    protected ModeledStore<Object> getStore(){
        return proxy.getTargetStore() != null ?
               proxy.getTargetStore() : 
               EmptyStore.get();
    }

    public CellViewBase getCellView(){
        return cellView;
    }
    
    @Override
    public Widget getWidget() {
        return cellView.getWidget();
    }

    
    // ListBox Columns
    
    
    @SuppressWarnings("unchecked")
    public StoreColumn<Object,Object> getValueColumn(){
        ColumnSet<? extends StoreColumn<Object,?>> columns = getStore().getModel().getColumns();
        switch (columns.size()){
            case 0:
                return EmptyStore.getEmptyColumn();
            default:
                return (StoreColumn<Object, Object>) columns.get(0);
        }
    }
    
    @SuppressWarnings("unchecked")
    public StoreColumn<Object,Object> getTextColumn(){
        ColumnSet<? extends StoreColumn<Object,?>> columns = getStore().getModel().getColumns();
        switch (columns.size()){
            case 0:
                return EmptyStore.getEmptyColumn();
            case 1:
                return (StoreColumn<Object, Object>) columns.get(0);
            default:
                return (StoreColumn<Object, Object>) columns.get(1);
        }
    }
    
}
